package org.jfk.sys.menu;

import org.apache.log4j.Logger;
import org.jfk.core.controller.BaseController;
import org.jfk.sys.user.User;

public class MenuController  extends BaseController {
	private static Logger logger = Logger.getLogger(MenuController.class);
	
	public void edit() {
		setAttr("menu", Menu.dao.findById(getParaToInt()));
	}	


	public void update(){
		Menu menu = getModel(Menu.class);
		menu.update();
		super.renderJson();
	}	
	
	public void save() throws Exception {
		
		Menu menu = getModel(Menu.class);
		if (menu.get("id") == null){
			User currUser = super.getCurrUser();
			menu.set("creator", currUser.getInt("id"));			
			
			menu.save();
		}else{
			menu.update();
		}
		super.renderJson();
	}	
	
	public void list(){
		listColumns = new String[][] { 
				{ "id", "编号" }, 
				{ "show_name", "菜单名称" },
				{ "url", "菜单URL" }, 
				{ "order_num_show", "显示顺序" }
				};
		
		super.setAttrs();
		//树形列表尽量不分页
		setAttr("page",Menu.dao.paginate(this));
		
	}



	public void del() {
		String id = getPara("id");
		Menu.dao.deleteById(id);
		super.renderText("test del");
	}

	
	public void delAll() {
		Integer[] checkedId = super.getParaValuesToInt("id[]");

		for (int i = 0; checkedId != null && i < checkedId.length; i++) {
			Menu.dao.deleteById(checkedId[i]);
		}
		super.renderText("test delAll");
	}
	
	public void addChild(){
		Menu menu = new Menu();
		menu.put("pid", getParaToInt());
		setAttr("menu", menu);
		super.renderJsp("edit.jsp");
	}

}
